using Single.Core.Service;
using Single.Sugar.Service;
using Single.Sys.Entity;
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Collections.Generic;
using System.Text;

namespace Single.Sys.Service
{
    public class SysConfigService : BaseService<SysConfig>
    {
        public SysConfig GetByCode(string code)
        {
            return rep.GetFirst(m => m.Code == code);
        }

        public List<SysConfig> GetByCodes(string[] codes)
        {
            return rep.GetList(m => codes.Contains(m.Code));
        }

        public List<SysConfig> GetByPrefix(string prefix)
        {
            return rep.GetList(m => m.Code.StartsWith(prefix));
        }

        public string GetContent(string code)
        {
            var entity = GetByCode(code);

            if (entity != null)
            {
                return entity.Content;
            }

            return string.Empty;
        }

        public long GetLongContent(string code)
        {
            long t = 0;

            var entity = GetByCode(code);

            if (entity != null)
            {
                long.TryParse(entity.Content, out t);
            }

            return t;
        }

        public int GetIntContent(string code)
        {
            int t = 0;

            var entity = GetByCode(code);

            if (entity != null)
            {
                int.TryParse(entity.Content, out t);
            }

            return t;
        }

        public decimal GetDecimalContent(string code)
        {
            decimal t = 0;

            var entity = GetByCode(code);

            if (entity != null)
            {
                decimal.TryParse(entity.Content, out t);
            }

            return t;
        }

        public double GetDoubleContent(string code)
        {
            double t = 0;

            var entity = GetByCode(code);

            if (entity != null)
            {
                double.TryParse(entity.Content, out t);
            }

            return t;
        }

        public DateTime GetDateTimeContent(string code)
        {
            DateTime t = DateTime.MinValue;

            var entity = GetByCode(code);

            if (entity != null)
            {
                DateTime.TryParse(entity.Content, out t);
            }

            return t;
        }

        public bool GetBoolContent(string code)
        {
            bool t = false;

            var entity = GetByCode(code);

            if (entity != null)
            {
                bool.TryParse(entity.Content, out t);
            }

            return t;
        }

        public ServiceResult SetContent(List<SysConfig> list)
        {
            var iList = new List<SysConfig>();
            var uList = new List<SysConfig>();

            foreach (var item in list)
            {
                var entity = GetByCode(item.Code);

                if (entity != null)
                {
                    entity.Title = item.Title;
                    entity.Content = item.Content;
                    entity.Other = item.Other;

                    uList.Add(entity);
                }
                else { iList.Add(item); }
            }

            var result = tenant.UseTran(() =>
            {
                rep.InsertRange(iList); rep.UpdateRange(uList);
            });

            return UnknownResult(result.IsSuccess);
        }
    }
}